<template>
    <div class="dict-type">
        <el-card class="!border-none" shadow="never">
            <el-form ref="formRef" class="mb-[-16px]" :model="queryParams" inline>
                <!-- <el-form-item class="w-[280px]" label="部门">
                    <el-input v-model="queryParams.name" clearable @keyup.enter="resetPage" />
                </el-form-item> -->
                <!-- <el-form-item class="w-[280px]" label="部门">
                    <el-select v-model="queryParams.status">
                        <el-option label="全部" value />
                        <el-option label="正常" :value="1" />
                        <el-option label="停用" :value="0" />
                    </el-select>
                </el-form-item>
                <el-form-item class="w-[280px]" label="部门状态">
                    <el-select v-model="queryParams.status">
                        <el-option label="全部" value />
                        <el-option label="正常" :value="1" />
                        <el-option label="停用" :value="0" />
                    </el-select>
                </el-form-item> -->
                <div class="xl:flex flex-row" style="flex-wrap: wrap;">
                    <template v-for="(item, index) in columnAuth" :key="index">
                        <el-form-item :label="item.name" :prop="item.column_name">
                            <div class="w-80">
                                <!-- 枚举类型 -->
                                <div v-if="enumerate.indexOf(item.column_name) != -1">
                                    <el-select v-model="queryParams[item.column_name]" :data-index="index" placeholder="请选择"
                                        size="large" style="width: 100%;">
                                        <el-option
                                            v-for="(item1, index1) in (dictOptions.options[item.column_name]?.options || [])"
                                            :label="item1.name" :value="item1.value" />
                                    </el-select>
                                </div>
                                <!-- 普通select -->
                                <div v-else-if="select.indexOf(item.column_name) != -1">
                                    <el-select v-model="queryParams[item.column_name]" :data-index="index" placeholder="请选择负责人"
                                        size="large" style="width: 100%;">
                                      
                                        <el-option v-for="(item2, index1) in admin.options" :label="item2.name"
                                            :value="item2.id" />
                                    </el-select>
                                </div>
                                <!-- 部门 -->
                                <div v-else-if="item.column_name == 'dept_id'">
                                    <el-select v-model="queryParams[item.column_name]" :data-index="index" placeholder="请选择"
                                        size="large" style="width: 100%;">
                                       
                                        <el-option v-for="(item1, index1) in deptOptions.options" :label="item1.name"
                                            :value="item1.id" />
                                    </el-select>
                                </div>

                                
                                <div v-else>
                                    <el-input v-model="queryParams[item.column_name]" :placeholder="'请输入' + item.name"
                                        type="data" show-word-limit clearable style="width: 100%;height: 40px;;" />
                                </div>
                            </div>
                        </el-form-item>
                    </template>
                </div>
                <el-form-item>
                    <el-button type="primary" @click="resetPage">查询</el-button>
                    <el-button @click="resetParams">重置</el-button>
                </el-form-item>
            </el-form>
        </el-card>
        <el-card class="!border-none mt-4" shadow="never">
            <div>
                <el-button v-perms="['information.information/getColumn']" type="primary" @click="handleAdd">
                    <router-link :to="{
                        path: getRoutePath('information.information/getColumn')
                    }">
                        <template #icon>
                            <icon name="el-icon-Plus" />
                        </template>
                        新增
                    </router-link>

                </el-button>
                <el-button v-perms="['setting.dict.dict_type/delete']" :disabled="!selectData.length" type="danger"
                    @click="handleDelete(selectData)">
                    <template #icon>
                        <icon name="el-icon-Delete" />
                    </template>
                    删除
                </el-button>
            </div>
            <div class="mt-4" v-loading="pager.loading">
                <div>
                    <el-table :data="pager.lists" size="large" @selection-change="handleSelectionChange">
                        <el-table-column type="selection" width="55" />
                        <!-- <el-table-column label="ID" prop="id" /> -->
                         <el-table-column label="日期" prop="date_time" min-width="180" />
                         <el-table-column label="学校" prop="school_name" min-width="120" />
                        <el-table-column label="姓名" prop="name" min-width="120" />
                        <el-table-column label="专业" prop="professional_name" min-width="120" />
                        <el-table-column label="学位" prop="is_degree" min-width="120" />
                        <el-table-column label="类型" prop="type" min-width="120" />
                        <el-table-column label="题目备注" prop="title_remark" min-width="120" />
                        <el-table-column label="题目" prop="write_status" min-width="120" />
                        <el-table-column label="字数" prop="paper_number" min-width="120" />
                        <el-table-column label="查重" prop="duplicate_checking" min-width="120" />
                        <el-table-column label="查重系统" prop="duplicate_checking_system" min-width="120" />
                        <el-table-column label="交稿材料" prop="submission_material" min-width="120" />
                        <el-table-column label="交稿时间" prop="first_commit_time" min-width="120" />
                        <el-table-column label="返修时间" prop="update_paper_time" min-width="120" />
                        <el-table-column label="操作" width="190" fixed="right">
                            <template #default="{ row }">
                                <el-button v-perms="['information.Information/edit']" link type="primary"
                                    >
                                    <router-link 
                                        :to="{
                                        path: getRoutePath('information.Information/edit'),
                                        query: {
                                            id: row.id
                                        }
                                    }">
                                        编辑
                                    </router-link>

                                </el-button>
                                <el-button v-perms="['information.Information/detail']" type="primary" link>
                                    <router-link :to="{
                                        path: getRoutePath('information.Information/detail'),
                                        query: {
                                            id: row.id
                                        }
                                    }">
                                        查看
                                    </router-link>
                                </el-button>
                                <el-button v-perms="['information.Information/detele']" link type="danger"
                                    @click="handleDelete(row.id)">
                                    删除
                                </el-button>
                            </template>
                        </el-table-column>
                    </el-table>
                </div>
                <div class="flex justify-end mt-4">
                    <pagination v-model="pager" @change="getLists" />
                </div>
            </div>
        </el-card>
        <!-- <edit-popup v-if="showEdit" ref="editRef" @success="getLists" @close="showEdit = false" /> -->
    </div>
</template>

<script lang="ts" setup name="dictType">
import { inforDelete, inforLists, getDict } from '@/api/perms/role'
import { deptAll } from '@/api/org/department'
import { usePaging } from '@/hooks/usePaging'
import { getRoutePath } from '@/router'
import feedback from '@/utils/feedback'
import { adminAll } from '@/api/perms/admin'
// import EditPopup from './edit.vue'
//枚举值
let enumerate = [
    'dept_status',
    'charge_of_group'
];
//下拉选择
let select = [
    'principal',
];
const editRef = shallowRef<InstanceType<typeof EditPopup>>()
const showEdit = ref(false)

interface QueryParam {
    name: string;
    column_name: string;
}

const columnAuth = reactive<QueryParam[]>([{
    name: '部门',
    column_name: 'dept_id',
},
{
    name: '编号',
    column_name: 'number',
},
{
    name: '学校',
    column_name: 'school_name',
},
{
    name: '姓名',
    column_name: 'name',
},
{
    name: '部门状态',
    column_name: 'dept_status',
},
{
    name: '负责组',
    column_name: 'charge_of_group',
},
{
    name: '负责人',
    column_name: 'principal',
}])
const queryParams = reactive({
    dept_id: '',
    number: '',
    school_name: '',
    name: '',
    dept_status: '',
    charge_of_group: '',
    principal: '',
})
const { pager, getLists, resetPage, resetParams } = usePaging({
    fetchFun: inforLists,
    params: queryParams
})

const selectData = ref<any[]>([])

const handleSelectionChange = (val: any[]) => {
    selectData.value = val.map(({ id }) => id)
}

// const handleAdd = async () => {
//     showEdit.value = true
//     await nextTick()
//     editRef.value?.open('add')
// }

// const handleEdit = async (data: any) => {
//     showEdit.value = true
//     await nextTick()
//     editRef.value?.open('edit')
//     editRef.value?.setFormData(data)
// }

// 删除角色
const handleDelete = async (id: any[] | number) => {
    await feedback.confirm('确定要删除？')
    await inforDelete({ id })
    getLists()
}
onActivated(() => {
    getLists()
})
const deptOptions = reactive({
    options: [],
});
const dictOptions = reactive<{
    options: {
        [key: string]: {
            options: Array<{
                name: string;
                value: string | number;
            }>;
        };
    };
}>({
    options: {},
})
const admin = reactive({
    options: [],
});
const getFoemData = async () => {
    let dict = await getDict();
    admin.options = await adminAll();
    //整理枚举类型字典数组
    dict.forEach((element: { type: string | number; options: Array<{ name: string; value: string | number }> }) => {
        dictOptions.options[element.type] = {
            options: element.options || []
        };
    })
    let dept = await deptAll();
    deptOptions.options = dept[0]['children'];
    console.log(toRaw(dictOptions),'YYYYYYYYYYYYYYYYYYYYYYYYYYYY',toRaw(deptOptions), toRaw(admin));
}
getFoemData()

</script>
